package com.bit.bit_forum.service.impl;

import com.bit.bit_forum.common.AppResult;
import com.bit.bit_forum.common.ResultCode;
import com.bit.bit_forum.dao.ArticleMapper;
import com.bit.bit_forum.dao.ArticleReplyMapper;
import com.bit.bit_forum.exception.ApplicationException;
import com.bit.bit_forum.model.Article;
import com.bit.bit_forum.model.ArticleReply;
import com.bit.bit_forum.service.IArticleReplyService;
import com.bit.bit_forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Autowired
    private ArticleReplyMapper articleReplyMapper;
    @Autowired
    private ArticleServiceImpl articleService;
    @Override
    public void create(ArticleReply articleReply) {
        //1.非空校验:
        if(articleReply==null || articleReply.getArticleId()==null
                || articleReply.getPostUserId()==null
                || StringUtil.isEmpty(articleReply.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值:
        articleReply.setReplyId(null);
        articleReply.setReplaceUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte)0);
        articleReply.setDeleteStat((byte)0);
        //        这个获取到的时间比当前时间要晚8小时
//        Date date = new Date();
        // 直接在Instant上增加8小时，再转回Date
        Date date = Date.from(
                Instant.now().plus(8, ChronoUnit.HOURS)
        );
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        //写入数据库:
        int n = articleReplyMapper.insertSelective(articleReply);
        System.out.println("n= " +n);
        if(n!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
//        修改帖子表的文章回复数:
        articleService.addReplyCountById(articleReply.getArticleId());

    }


    /**
     *
    //    查询回复列表(回复表+用户表_)
     * @param articleId 帖子id
     * @return
     */
    @Override
    public List<ArticleReply> selectAllByArticleId(Long articleId) {
//        参数校验:
        if(articleId==null || articleId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        对文章校验:
//        Article article = articleService.selectById(articleId);
//        if(article==null || article.getDeleteStat()==1){
//            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
//        }
//        if(article.getState()==1){
//            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
//        }
//        调用DAO:
        List<ArticleReply> articleReplies = articleReplyMapper.selectAllByArticleId(articleId);
        return articleReplies;
    }
}
